package com.shengdong.water.feature.signIn

import android.text.TextUtils
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.kongzue.dialogx.dialogs.WaitDialog
import com.shengdong.water.R
import com.shengdong.water.base.BaseVMActivity
import com.shengdong.water.code.adapter.ImageGridAdapter
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.router.RouterManager
import com.shengdong.water.code.utils.GridSpaceItemDecoration
import com.shengdong.water.code.viewmodel.SignDetailViewModel
import com.shengdong.water.databinding.ActivityRecordDetailBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.json.JSONObject

@Route(path = RouterConfig.SignIn.RECORD_DETAIL_PATH)
@AndroidEntryPoint
class SignRecordDetailActivity :
    BaseVMActivity<ActivityRecordDetailBinding, SignDetailViewModel>() {

    override fun getLayoutId() = R.layout.activity_record_detail

    override fun getViewModelClass() = SignDetailViewModel::class.java

    private val imageAdapter = ImageGridAdapter()

    override fun initViews() {
        super.initViews()
        setTitle("签到详情")
        binding.viewModel = viewModel

        binding.recyclerPictures.layoutManager = GridLayoutManager(this, 3)
        binding.recyclerPictures.addItemDecoration(GridSpaceItemDecoration(spacing = 10))
        binding.recyclerPictures.adapter = imageAdapter
    }


    override fun showLoading(loading: String) {
        WaitDialog.show(loading)
    }

    override fun dismissLoading() {
        super.dismissLoading()
        WaitDialog.dismiss()
    }

    override fun initData() {
        super.initData()
        viewModel.getSignDetail(intent.getStringExtra("id") ?: "")
    }

    override fun observeData() {
        lifecycleScope.launch {
            viewModel.detail.collect { it ->

                it?.let {
                    binding.livSignDate.setContent(it.checkInTime)
                    if(!TextUtils.isEmpty(it.telphone)){
                        binding.livPhone.setContent(it.telphone)
                    }
                    binding.livLocation.setContent(it.signLon+ "," + it.signLat)
                    binding.livOutletName.setContent(it.dischargeOutletName)
                    binding.livSignName.setContent(it.signAccount)
                    binding.livRemark.setContent(it.remark)
                    if(it.figure.isNotEmpty()){
                        imageAdapter.setItems(it.figure.split(","))
                    }
                    if(it.fillIn.isNotEmpty()){
                        val jsonObject = JSONObject(it.fillIn)
                        val iterator = jsonObject.keys().iterator()

                        while (iterator.hasNext()) {
                            val key = iterator.next()
                            val value = jsonObject.getString(key)
                            println("Key: $key, Value: $value")
                            if (TextUtils.isEmpty(value)) {
                                binding.livCod.setContent("0mg/L")
                                binding.livAmmoniaNitrogen.setContent("0mg/L")
                                binding.livPhosphorus.setContent("0mg/L")
                                binding.livNitrogen.setContent("0mg/L")
                                binding.livPh.setContent("0PH")
                                binding.livTemperature.setContent("0℃")
                                binding.livOxygen.setContent("0mg/L")
                                binding.livTurbidity.setContent("0ntu")
                                binding.livConductivity.setContent("0uS/cm")
                            }else {
                                binding.livCod.setContent("${value}mg/L")
                                binding.livAmmoniaNitrogen.setContent("${value}mg/L")
                                binding.livPhosphorus.setContent("${value}mg/L")
                                binding.livNitrogen.setContent("${value}mg/L")
                                binding.livPh.setContent("${value}PH")
                                binding.livTemperature.setContent("${value}℃")
                                binding.livOxygen.setContent("${value}mg/L")
                                binding.livTurbidity.setContent("${value}ntu")
                                binding.livConductivity.setContent("${value}uS/cm")
                            }
                        }

                    }


                }


            }
        }
    }


    override fun handleBackPress() {
    }


}